package com.dpp.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.dpp.context.BaseContext;
import com.dpp.controller.user.OrderController;
import com.dpp.entity.AddressBook;
import com.dpp.entity.Orders;
import com.dpp.entity.ProductInfo;
import com.dpp.entity.Users;
import com.dpp.mapper.*;
import com.dpp.result.Result;
import com.dpp.service.UserMoneyService;
import com.dpp.vo.HomeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 定时任务类，定时处理订单状态
 */
@Component
@Slf4j
public class OrderTask {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMoneyService userMoneyService;

    @Autowired
    private OrderController orderController;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private TakeOrderMapper takeOrderMapper;

    @Autowired
    private ProductInfoMapper productInfoMapper;
    /**
     * 处理超时订单的方法
     */
    @Scheduled(cron = "0 * * * * ?") //每分钟触发一次
    public void processTimeoutOrder(){
        log.info("定时处理超出订单：{}", LocalDateTime.now());

        LocalDateTime time = LocalDateTime.now().plusMinutes(-60);

        List<Orders> ordersList = orderMapper.selectOutTime(Orders.REACHED,time);

        if(ordersList != null && !ordersList.isEmpty()){
            for (Orders orders : ordersList) {
                orders.setOrderStatus(Orders.COMPLETED);
                orders.setUpdateTime(LocalDateTime.now());
                orders.setFinishTime(LocalDateTime.now());
                userMoneyService.save(orders);
                orderMapper.updateOrders(orders);
            }
        }
    }
    @Scheduled(cron = "30 0/10 17-18 * * ?")
    public void takeDiscountOrder17() throws ParseException {
//        log.info("自动接单：{}", LocalDateTime.now());
//        List<Orders> orders = orderMapper.selectByOpenIdAll1();
//        String openid="oD1dE6RdILU9O-EXNaaOSc4cK2-g";
//        for (Orders homeVO : orders) {
//            takeOrder(Math.toIntExact(homeVO.getId()),openid);
//        }
    }

    @Scheduled(cron = "0 0 0/2 * * ?")
    public void takeDiscountOrder() throws ParseException {
//        log.info("自动接单：{}", LocalDateTime.now());
//        List<Orders> orders = orderMapper.selectByOpenIdAll1();
//        String openid="oD1dE6RdILU9O-EXNaaOSc4cK2-g";
//        for (Orders homeVO : orders) {
//            takeOrder(Math.toIntExact(homeVO.getId()),openid);
//        }
    }

    public Result<String> takeOrder(Integer id,String openid) {
        Orders order = orderMapper.selectById(id);
        Long number = order.getNumber();
        //插入到takeOrders表中
        takeOrderMapper.insert(number, openid, LocalDateTime.now());
        //更新订单状态为已接单
        orderMapper.updateOrderType(id);
        return Result.success("接单成功");
    }


}
